// pages/wxBind/wxBind.js
const util = require('../../utils/util.js')
const networkUtil=require('../../utils/networkutil.js')

const app = getApp()
let logStrPrefix="wxBind"

Page({
    /**
     * 页面的初始数据
     */
    data: {
        //一卡通用户绑定相关变量
        bindtype: 0,
        username: '',
        password: '',

        //普通员工绑定相关变量
        employeeno: '',
        phonenum: '',
        vcode: '',
        vcodeTip: '获取验证码',
    },

    selectType: function(event) {
        this.setData({
            bindtype: event.currentTarget.dataset.bindtype,
        })
    },

    bindKeyInput: function(event) {
        let name = event.currentTarget.dataset.name;
        this.setData({
            [name]: event.detail.value,
        });
        //console.log(this.data[name]);
    },

    /**
     * 获取验证码
     */
    getVCode: function () {
        let that = this

        //若that.data.vcodeTip为'获取验证码'
        if (that.data.vcodeTip == '获取验证码') {
            let url = app.globalData.baseUrl +"/user/getVCode"
            let requestData={
                phoneNo: that.data.phonenum
            }
            
            networkUtil.request_get(url,requestData).then((res)=>{
                //若已获取验证码
                let countDownNum=60
                let timerInterval=setInterval(()=>{
                    countDownNum--
                    that.setData({
                        vcodeTip: countDownNum + " s"
                    })
                    //若countDownNum为0
                    if(countDownNum==0){
                        clearInterval(timerInterval)
                        that.setData({
                            vcodeTip: "获取验证码"
                        })
                    }
                }, 1000)
                
            }).catch((res)=>{
                //若未获取验证码
                console.error(logStrPrefix+" 获取验证码出错!", res)
                util.showToast("获取验证码出错!")
            })
        }
    },
    
    /**
     * 提交用户绑定信息。
     */
    submitForm: function (event) {
        let that=this

        //若that.data.bindtype为0，绑定方式为"一卡通用户绑定"
        if(that.data.bindtype==0){
            let url = app.globalData.baseUrl +"/user/wxBindUser"
            let requestData={
                type: 0,
                openId: app.globalData.openId,
                username: that.data.username,
                password: that.data.password
            }

            networkUtil.request_get(url,requestData).then((res)=>{
                //若绑定成功
                util.showToast("用户绑定成功!")
                setTimeout(()=>{
                    wx.redirectTo({
                        url: '../index/index'
                    })
                },1000)
            }).catch((res)=>{
                //若绑定出错
                console.error(logStrPrefix+" 一卡通用户绑定出错!",res)
                util.showToast("用户绑定出错!")
            })
        }

        //若that.data.bindtype为1，绑定方式为"普通员工绑定"
        if (that.data.bindtype == 1){
            let url = app.globalData.baseUrl +"/user/wxBindUser"
            let requestData={
                type: 1,
                openId: app.globalData.openId,
                employeeNo: that.data.employeeno,
                phoneNo: that.data.phonenum,
                vCode: that.data.vcode
            }

            networkUtil.request_get(url,requestData).then((res)=>{
                //若绑定成功
                util.showToast("用户绑定成功!")
                setTimeout(() => {
                    wx.redirectTo({
                        url: '../index/index'
                    })
                }, 1000)
            }).catch((res)=>{
                //若绑定出错
                console.error(logStrPrefix + " 普通员工绑定出错!", res)
                util.showToast("用户绑定出错!")
            })
        }
    },





    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})